<template>
  <el-dialog title="设置权限" :visible="showDialogPerm" append-to-body @close="hClose">
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-button @click="hSave">确定</el-button>

  </el-dialog>
</template>

<script>
import { getPermission, assignPerm } from '@/api/permission'
import { arr2tree } from '@/utils'
import { getRoleDetail } from '@/api/role'
export default {
  props: {
    showDialogPerm: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permissionData: []
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    hClose() {
      this.$emit('update:show-dialog-perm', false)
    },
    async loadPermission() {
      const res = await getPermission()
      console.log(res)
      const Data = res.data
      this.permissionData = arr2tree(Data, 0)
    },
    async getPermiss() {
      const res = await getRoleDetail(this.roleId)
      // 数据回填
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async hSave() {
      const keys = this.$refs.tree.getCheckedKeys()
      //   console.log(keys)
      await assignPerm({ id: this.roleId, permIds: keys })
      this.$message.success('权限设置成功')
      this.hClose()
    }

  }
}
</script>

<style>

</style>
